package com.xy.service.approval;

import com.xy.api.constant.FieldTypeConstant;
import com.xy.api.exception.BizException;
import com.xy.api.model.dto.approval.ApprovalInstanceDTO;
import com.xy.api.model.dto.approval.ApprovalTemplateDTO;
import com.xy.api.model.dto.approval.ApprovalTemplateInfoFieldDTO;
import com.xy.api.model.dto.common.FieldValueDTO;
import com.xy.api.model.request.approval.ApplyApprovalRequest;
import com.xy.api.model.request.approval.ApprovalInstanceRequest;
import com.xy.api.service.approval.IApprovalInstanceService;
import com.xy.api.service.approval.IApprovalTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ApprovalInstanceServiceImpl implements IApprovalInstanceService {

    @Autowired
    private IApprovalTemplateService approvalTemplateService;

    @Override
    public void apply(ApplyApprovalRequest request) {
        ApprovalTemplateDTO approvalTemplate = approvalTemplateService.findDetailById(request.getApprovalTemplateId());
        checkInfoFieldValues(request, approvalTemplate);


    }

    /**
     * 校验申请的值信息
     *
     * @param request
     * @param approvalTemplate
     */
    private void checkInfoFieldValues(ApplyApprovalRequest request, ApprovalTemplateDTO approvalTemplate) {
        if (null == approvalTemplate || approvalTemplate.getDisuse()) {
            throw new BizException("模板不存在(或已废弃)");
        }
        List<ApprovalTemplateInfoFieldDTO> approvalTemplateInfoFieldList = approvalTemplate.getApprovalTemplateInfoFieldList();
        if (!CollectionUtils.isEmpty(approvalTemplateInfoFieldList)) {
            List<ApprovalTemplateInfoFieldDTO> mustRequiredFieldList = approvalTemplateInfoFieldList.stream().filter(e -> e.getIsRequired()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(mustRequiredFieldList)) {
                if (CollectionUtils.isEmpty(request.getValues())) {
                    throw new BizException("申请信息未填写");
                }
                if (!approvalTemplate.getSupportMultiple() && request.getValues().size() > 1) {
                    throw new BizException("申请信息仅支持申请一份");
                }
                Map<Long, ApprovalTemplateInfoFieldDTO> mustRequiredFieldMap = mustRequiredFieldList.stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
                for (List<FieldValueDTO> value : request.getValues()) {
                    for (FieldValueDTO fieldValue : value) {
                        Long fieldId = fieldValue.getFieldId();
                        if (mustRequiredFieldMap.containsKey(fieldId)) {
                            if (!StringUtils.hasText(fieldValue.getFieldValue())) {
                                throw new BizException("申请信息未填写[" + fieldValue.getFieldName() + "]");
                            }
                            ApprovalTemplateInfoFieldDTO approvalTemplateInfoField = mustRequiredFieldMap.get(fieldId);
                            Integer fieldType = approvalTemplateInfoField.getFieldType();
                            if (Objects.equals(fieldType, FieldTypeConstant.FIELD_TYPE_OPTION)) {
                                Set<String> optionValues = approvalTemplateInfoField.getFieldOptionList().stream().map(e -> e.getFieldOptionValue()).collect(Collectors.toSet());
                                if (!optionValues.contains(fieldValue.getFieldValue())) {
                                    throw new BizException("申请信息类型为枚举类型，枚举值不合法[" + fieldValue.getFieldName() + "]");
                                }
                            }
                            //todo 后面优化可能要使用正则表达式校验值
                        }
                    }
                }
            }
        }
    }

    @Override
    public ApprovalInstanceDTO detail(ApprovalInstanceRequest request) {
        return null;
    }

    @Override
    public void quash(ApprovalInstanceRequest request) {

    }
}
